package com.example.activitysign.service.impl;

import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.activitysign.entity.ActivityAward;
import com.example.activitysign.mapper.ActivityAwardMapper;
import com.example.activitysign.mapper.ActivityAwardRuleMapper;
import com.example.activitysign.mapper.ActivityUserAwardRecordMapper;
import com.example.activitysign.pages.PageResult;
import com.example.activitysign.pages.PageValidate;
import com.example.activitysign.service.IActivityAwardService;
import com.example.activitysign.validate.ActivityAwardCreateValidate;
import com.example.activitysign.validate.ActivityAwardSearchValidate;
import com.example.activitysign.validate.ActivityAwardUpdateValidate;
import com.example.activitysign.vo.activity.ActivityAwardDetailVo;
import com.example.activitysign.vo.activity.ActivityAwardListedVo;
import com.example.activitysign.vo.activity.ActivityUserAwardVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 奖品实现类
 *
 * @author fei
 */
@Slf4j
@Service
public class ActivityAwardServiceImpl extends ServiceImpl<ActivityAwardMapper, ActivityAward> implements IActivityAwardService {

    @Resource
    private ActivityAwardMapper activityAwardMapper;
    @Resource
    private ActivityAwardRuleMapper activityAwardRuleMapper;
    @Resource
    private ActivityUserAwardRecordMapper activityUserAwardRecordMapper;
    @Resource
    private DefaultIdentifierGenerator defaultIdentifierGenerator;


    /**
     * 该奖品获得列表
     *
     * @param pageValidate
     * @param searchValidate
     * @return
     */
    @Override
    public PageResult<ActivityUserAwardVo> userGetList(PageValidate pageValidate, ActivityAwardSearchValidate searchValidate) {
        Page<ActivityAward> page = new Page<>(pageValidate.getPageNo(), pageValidate.getPageSize());
        IPage<ActivityUserAwardVo> list = activityUserAwardRecordMapper.userGetList(page, searchValidate);
        return PageResult.iPageHandle(list.getTotal(), list.getCurrent(), list.getSize(), list.getRecords());

    }

    @Override
    public PageResult<ActivityAwardListedVo> list(PageValidate pageValidate, ActivityAwardSearchValidate searchValidate) {
        return null;
    }

    /**
     * 奖品详情
     *
     * @param id 主键参数
     * @return ActivityAward
     * @author fei
     */
    @Override
    public ActivityAwardDetailVo detail(Long id) {
        ActivityAward model = activityAwardMapper.selectById(id);
        Assert.notNull(model, "数据不存在");

        ActivityAwardDetailVo vo = new ActivityAwardDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 奖品新增
     *
     * @param createValidate 参数
     * @author fei
     */
    @Override
    public void add(ActivityAwardCreateValidate createValidate) {
        ActivityAward model = new ActivityAward();
        BeanUtils.copyProperties(createValidate, model);
        model.setId(defaultIdentifierGenerator.nextId(model));
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        activityAwardMapper.insert(model);
    }

    /**
     * 奖品编辑
     *
     * @param updateValidate 参数
     * @author fei
     */
    @Override
    public void edit(ActivityAwardUpdateValidate updateValidate) {
        ActivityAward model = activityAwardMapper.selectById(updateValidate.getId());
        Assert.notNull(model, "数据不存在!");
        BeanUtils.copyProperties(updateValidate, model);
        model.setUpdateTime(new Date());
        activityAwardMapper.updateById(model);
    }

    /**
     * 奖品删除
     *
     * @param id 主键ID
     * @author fei
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(Long id) {
        int i = activityAwardMapper.deleteById(id);
        //删除奖品规则
        activityAwardRuleMapper.deleteByAwardId(id);
        Assert.isTrue(i > 0, "数据不存在!");
    }

}

